import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import router from "@/router/router.js";
import pinia from "@/stores/pinia.js";
import { useUserStore } from "@/stores/user.js";
import { useEmployeeStore } from "@/stores/employee.js";
import { useAdminStore } from "@/stores/admin.js";

// 主文件
const app = createApp(App);
app.use(router);                 // 路由
app.use(pinia);                  // 状态管理
app.use(ElementPlus);            // Element Plus组件库
app.mount("#app");   // 挂载

const user = useUserStore();
const employee = useEmployeeStore();
const admin = useAdminStore();

// 导航守卫
router.beforeEach(function (to, from) {
    if(to.meta.userAuth) {
        if(user.user_id === null) {
            //alert();
            return {
                path: '/user/index'
            }
        }
    }
    if(to.meta.employeeAuth) {
        if(employee.employee_id === null) {
            //alert();
            return {
                path: '/employee/index'
            }
        }
    }
    if(!to.meta.employeeAuth) {
        if(employee.employee_id !== null) {
            //alert();
            return {
                path: '/employee/roomstatus'
            }
        }
    }
    if(to.meta.adminAuth) {
        if(admin.admin_id === null) {
            //alert();
            return {
                path: '/admin/index'
            }
        }
    }
    if(!to.meta.adminAuth) {
        if(admin.admin_id !== null) {
            //alert();
            return {
                path: '/admin/history'
            }
        }
    }
});
